#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
	pid_t id = fork();
	if (id == 0) {
		// child process
		int count = 3;
		while (count) {
			printf("child process do his things.....PID:%d, PPID:%d, count:%d\n", getpid(), getppid(), count);
			sleep(3);
            count--;
		}
		exit(0);
	}
	// father process
	while (1) {
		int status = 0;
		pid_t ret = waitpid(id, &status, WNOHANG);
		if (ret > 0) {
			printf("wait child process success\n");
			printf("child process exit code:%d\n", WEXITSTATUS(status));
			break;
		}
		else if (ret == 0) {
			printf("father process do his things.....\n");
			sleep(1);
		}
		else {
			printf("waitpid error...\n");
			break;
		}
	}
	return 0;
}